package com.yex.service.otc.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.dao.otc.OtcOrderDao;
import com.yex.utils.JsonResultVo;
import com.yex.entity.otc.OtcEntrust;
import com.yex.entity.otc.OtcOrder;
import com.yex.entity.otc.OtcVoucher;
import com.yex.dao.otc.OtcEntrustDao;
import com.yex.dao.otc.OtcVoucherDao;
import com.yex.service.otc.IOtcVoucherService;
import com.yex.utils.vo.OtcOperEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2018-05-04
 */
@Service
public class OtcVoucherServiceImpl extends ServiceImpl<OtcVoucherDao, OtcVoucher> implements IOtcVoucherService {
	
	@Autowired
    private OtcOrderDao otcOrderMapper;
    @Autowired
    private OtcEntrustDao otcEntrustMapper;
    

	@Override
	public JsonResultVo selectVoucherList(OtcVoucher otcVoucher) {
		OtcOrder otcOrder = otcOrderMapper.selectById(otcVoucher.getOrderId()); //根据订单id查询订单
        OtcEntrust otcEntrust = otcEntrustMapper.selectById(otcOrder.getEntrustId());  //根据委托id查询，委托信息
        if(otcVoucher.getUserId() != null) {
        	if(!otcOrder.getUserId().equals(otcVoucher.getUserId())&& !otcEntrust.getUserId().equals(otcVoucher.getUserId())) {
        		new JsonResultVo(true,"获取成功",0);
        	}
        }
        
		List<OtcVoucher> otcVoucherList = baseMapper.selectVoucherListByOrderId(otcVoucher);
		for(OtcVoucher v : otcVoucherList) {
			//v.setImg(commConfig.getDomain()+v.getImg());
		}
		JsonResultVo vo = new JsonResultVo(true,"获取成功",otcVoucherList,0);
		return vo;
	}

	@Override
	@Transactional
	public JsonResultVo doDelVoucher(OtcVoucher otcVoucher) {
		OtcVoucher queryOtcVoucher = baseMapper.selectById(otcVoucher.getId());
		OtcOrder otcOrder = otcOrderMapper.selectById(queryOtcVoucher.getOrderId()); //根据订单id查询订单
		JsonResultVo vo = new JsonResultVo(true,"删除成功",0);
		if(otcOrder.getStatus()!=OtcOperEnum.SELLER_COMPLAINT) {
			vo = new JsonResultVo(true,"不可删除",-1);
			return vo;
		}
		queryOtcVoucher.setIsDelete(1);
		int result = baseMapper.updateById(queryOtcVoucher);
		if(result<=0) {
			vo = new JsonResultVo(true,"删除失败",-1);
		}
		return vo;
	}

}
